
/*
 * Copyright (C) 2010 Bitglue. All rights reserved.
 *
 * $Id: TransformUtils.java bself $
 */

package com.bitglue.web.admin.util;

import java.io.OutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import static javax.xml.transform.OutputKeys.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * TransformUtils for repeatable transform code.
 * @author Bryan Patrick Self
 */
public abstract class TransformUtils {

    private TransformUtils() {
    }

    public static void transform(Document doc, OutputStream os)
      throws Exception {
        Transformer xf = TransformerFactory.newInstance().newTransformer();
        xf.setOutputProperty(OMIT_XML_DECLARATION, "yes");
        xf.transform(new DOMSource(doc), new StreamResult(os));
    }

    public static void transform(Element elm, OutputStream os)
      throws Exception {
        transform(elm.getOwnerDocument(), os);
    }

}
